﻿package common.utils.dt
{
    import flash.events.*;
    import flash.utils.*;

    public class RealTimer extends EventDispatcher
    {
        private var timer:Timer = null;
        private var _totalTime:int = 0;
        private var startTime:int = 0;
        private var unitTimeSpan:int = 0;
        private var accurateValue:int = 0;
        private var stepTimes:Array = null;
        private var _pastUnitCount:int = 0;
        private var lastTickPastUnitCount:int = 0;
        private var step:uint = 0;
        private var isStepOver:Boolean = false;

        public function RealTimer(param1:int = 0, param2:Array = null, param3:int = 1000, param4:int = 2)
        {
            this.stepTimes = param2;
            if (!this.stepTimes)
            {
                this.isStepOver = true;
            }
            this.unitTimeSpan = param3;
            this.totalTime = param1;
            this.accurateValue = param4;
            this.timer = new Timer(this.unitTimeSpan / this.accurateValue, int.MAX_VALUE);
            this.timer.addEventListener(TimerEvent.TIMER, this.onTick);
            return;
        }

        public function set totalTime(param1:int) : void
        {
            this._totalTime = param1;
            return;
        }

        public function get totalTime() : int
        {
            return this._totalTime;
        }

        public function start() : void
        {
            this.startTime = getTimer();
            this.timer.start();
            return;
        }

        public function stop() : void
        {
            this.timer.stop();
            return;
        }

        public function reset() : void
        {
            var _loc_1:int = 0;
            this._pastUnitCount = 0;
            this.lastTickPastUnitCount = _loc_1;
            this.timer.stop();
            this.timer.reset();
            return;
        }

        private function getPastTime() : Number
        {
            return getTimer() - this.startTime;
        }

        public function get pastUnitCount() : int
        {
            return this._pastUnitCount;
        }

        public function get pastUnitCountAfterStep() : int
        {
            if (this.step == 0)
            {
                return this._pastUnitCount;
            }
            return this._pastUnitCount - this.stepTimes[this.step] / this.unitTimeSpan;
        }

        public function get pastTime() : Number
        {
            return this._pastUnitCount * this.unitTimeSpan;
        }

        public function get pastTimeAfterStep() : Number
        {
            if (this.step == 0)
            {
                return this._pastUnitCount * this.unitTimeSpan;
            }
            return this._pastUnitCount * this.unitTimeSpan;
        }

        public function get residueTime() : Number
        {
            return this.totalTime - this.pastTime;
        }

        public function get residueTimeForStep() : Number
        {
            if (this.isStepOver)
            {
                return 0;
            }
            return this.stepTimes[this.step] - this.pastTimeAfterStep;
        }

        public function get residueUnitCount() : int
        {
            return (this.totalTime / this.unitTimeSpan >> 0) - this._pastUnitCount;
        }

        public function get residueUnitCountForStep() : int
        {
            return this.stepTimes[this.step] / this.unitTimeSpan >> -this._pastUnitCount;
        }

        private function onTick(event:TimerEvent) : void
        {
            var _loc_2:int = 0;
            this._pastUnitCount = this.getPastTime() / this.unitTimeSpan >> 0;
            if (!this.isStepOver)
            {
                if (this._pastUnitCount > this.stepTimes[this.step] / this.unitTimeSpan >> 0)
                {
                    this.dispatchEvent(new RealTimerEvent(RealTimerEvent.StepTimeup, this.step));
                    var _loc_3:RealTimer = this;
                    var _loc_4:* = this.step + 1;
                    _loc_3.step = _loc_4;
                }
                if (this.step > this.stepTimes.length)
                {
                    this.dispatchEvent(new RealTimerEvent(RealTimerEvent.StepOver));
                    this.isStepOver = true;
                }
            }
            if (this._pastUnitCount > this.lastTickPastUnitCount)
            {
                _loc_2 = this._pastUnitCount - this.lastTickPastUnitCount;
                this.dispatchEvent(new RealTimerEvent(RealTimerEvent.RealTimer, 0, _loc_2));
                this.lastTickPastUnitCount = this._pastUnitCount;
            }
            if (this.getPastTime() >= this.totalTime)
            {
                this.dispatchEvent(new RealTimerEvent(RealTimerEvent.Complete));
            }
            return;
        }

    }
}
